//
//  YDNewContactTableViewCell.m
//  IngCard
//
//  Created by kf on 2017/9/21.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDNewContactTableViewCell.h"

@implementation YDNewContactCellDataSource

-(Class) viewClass
{
    return [YDNewContactTableViewCell class];
}

@end


@interface YDNewContactTableViewCell()
@property(nonatomic, strong) UIView *backView;

@property(nonatomic, strong) UIButton *checkButton;
@property(nonatomic, strong) UIImageView *headerImageView;
@property(nonatomic, strong) UILabel *nameLabel;
@property(nonatomic, strong) UILabel *companyLabel;
@property(nonatomic, strong) UILabel *occupationLabel;

@end

@implementation YDNewContactTableViewCell


+(CGFloat)viewHeight:(YDNewContactCellDataSource *) dataSource
{
    return dataSource.height ?:  80;
}

- (void) initSubviews
{
    [super initSubviews];
    self.contentView.backgroundColor = UIColorMake(245, 245, 245);
    
    [self.contentView addSubview:self.backView];
    [self.backView makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.contentView);
        make.width.equalTo(self.contentView).offset(-56);
        make.height.equalTo(self.contentView).offset(-2);
    }];
    
    [self.contentView addSubview:self.checkButton];
    [self.checkButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.backView.mas_left).offset(7);
    }];
    
    [self.contentView addSubview:self.headerImageView];
    [self.headerImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.checkButton.mas_right).offset(17);
        make.height.equalTo(36);
        make.width.equalTo(36);
    }];

    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerImageView.mas_top);
        make.left.equalTo(self.headerImageView.mas_right).offset(34);
    }];

    [self.contentView addSubview:self.companyLabel];
    [self.companyLabel makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.headerImageView.mas_bottom);
        make.left.equalTo(self.nameLabel.mas_left);
    }];

    [self.contentView addSubview:self.occupationLabel];
    [self.occupationLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.companyLabel);
        make.left.equalTo(self.companyLabel.mas_right).offset(6);
    }];
}

-(void) onViewModelChanged
{
    YDNewContactCellDataSource *dataSource = (YDNewContactCellDataSource *)self.dataSource;
    if (dataSource)
    {
        UIImageViewSetStr(self.headerImageView, dataSource.contact.headImage);
        self.nameLabel.text = NSStringFormat(@"%@ %@",dataSource.contact.firstName, dataSource.contact.lastName);
        [self.nameLabel sizeToFit];
        
        self.companyLabel.text = dataSource.contact.company;
        [self.companyLabel sizeToFit];
        
        self.occupationLabel.text = dataSource.contact.occupation;
        [self.occupationLabel sizeToFit];
    }
}

- (void)checkButtonAction:(UIButton *)btn
{
    YDNewContactCellDataSource *dataSource = (YDNewContactCellDataSource *)self.dataSource;
    if (dataSource)
    {
        dataSource.beSelected = !dataSource.beSelected;
        self.checkButton.selected = !self.checkButton.selected;
    }
}


#pragma mark - lazy loading

-(UIView *)backgroundView
{
    if (!_backView)
    {
        _backView = [UIView new];
        _backView.layer.cornerRadius = 6.0f;
        
        [_backView setBackgroundColor:UIColorRed];
        _backView.tintColor = UIColorRed;
//        _backView.alpha = 1.0;
        _backView.backgroundColor = UIColorRed;// UIColorMake(255, 255, 255);
    }
    return _backView;
}

-(UIButton *) checkButton
{
    if (!_checkButton)
    {
        _checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_checkButton setBackgroundImage:UIImageMake(@"contact_new_unSelected")
                                forState:UIControlStateNormal];
        
        [_checkButton setBackgroundImage:UIImageMake(@"contact_new_selected")
                                forState:UIControlStateSelected];
        _checkButton.selected = NO;
        [_checkButton addTarget:self action:@selector(checkButtonAction:)
                     forControlEvents:UIControlEventTouchUpInside];
    }
    return _checkButton;
}

-(UIImageView *)headerImageView
{
    if (!_headerImageView)
    {
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
        _headerImageView.image = UIImageMake(@"myCardCell_userHeader1");
        _headerImageView.layer.cornerRadius = 18;
        _headerImageView.layer.masksToBounds = YES;
        _headerImageView.contentMode = UIViewContentModeScaleToFill;
        [_headerImageView sizeToFit];
    }
    return _headerImageView;
}

-(UILabel *)nameLabel
{
    if (!_nameLabel)
    {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"Andrea Berry";
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.font = UIRegularFontMake(14);
        _nameLabel.textColor = UIColorMake(0, 0, 0);
        [_nameLabel sizeToFit];
    }
    return _nameLabel;
}

-(UILabel *)companyLabel
{
    if (!_companyLabel)
    {
        _companyLabel = [[UILabel alloc] init];
        _companyLabel.text = @"Andrea Berry";
        _companyLabel.textAlignment = NSTextAlignmentLeft;
        _companyLabel.font = UIMediumFontMake(12);
        _companyLabel.textColor = UIColorMake(155, 155, 155);
        [_companyLabel sizeToFit];
    }
    return _companyLabel;
}

-(UILabel *)occupationLabel
{
    if (!_occupationLabel)
    {
        _occupationLabel = [[UILabel alloc] init];
        _occupationLabel.text = @"Andrea Berry";
        _occupationLabel.textAlignment = NSTextAlignmentLeft;
        _occupationLabel.font = UIMediumFontMake(12);
        _occupationLabel.textColor = UIColorMake(155, 155, 155);
        [_occupationLabel sizeToFit];
    }
    return _occupationLabel;
}

@end
